package com.example.b_308.myapplication.view.main.Base;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.model.Staff;

import java.util.List;

/**
 * Created by B-308 on 2017/11/24.
 */
public class AddressAdapter extends BaseAdapter {

    List<Staff> lists;
    Context context;
    LayoutInflater inflater;

    public static final int TYPE_SEARCH = 0;
    public static final int TYPE_MENU = 1;

    public AddressAdapter(List<Staff> lists, Context context, IAddressClick iAddressClick) {
        this.lists = lists;
        this.context = context;
        this.iAddressClick = iAddressClick;
        this.inflater = LayoutInflater.from(context);
    }

    public static final int TYPE_TIME_STAFF=2;

    public int getItemViewType(int position){
        switch (position){
            case 0:
                return TYPE_SEARCH;
            case 1:
                return TYPE_MENU;
            default:
                return TYPE_TIME_STAFF;
        }
    }

    @Override
    public int getCount() {
        return lists.size()+3;
    }

    @Override
    public Staff getItem(int position) {
        if(position > 1) {
            return lists.get(position-2);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseLVHolder baseLVHolder;
            switch (getItemViewType(position)){
                case TYPE_SEARCH:
                    if(null == convertView || !(convertView.getTag() instanceof SearchViewHolder)){
                        convertView = inflater.inflate(R.layout.item_search,null,false);
                        baseLVHolder = new SearchViewHolder(convertView);
                        convertView.setTag(baseLVHolder);
                    }
                    break;
                case TYPE_MENU:
                    if(null == convertView || !(convertView.getTag() instanceof MenuViewHolder)){
                        convertView = inflater.inflate(R.layout.item_menu,null,false);
                        baseLVHolder = new MenuViewHolder(convertView);
                        convertView.setTag(baseLVHolder);
                    }
                    break;
                default:
                    if(null == convertView || !(convertView.getTag() instanceof StaffViewHolder)){
                        convertView = inflater.inflate(R.layout.item_staff,null,false);
                        baseLVHolder = new StaffViewHolder(convertView);
                        convertView.setTag(baseLVHolder);
                    }else{
                        baseLVHolder = (BaseLVHolder) convertView.getTag();
                        if(baseLVHolder instanceof StaffViewHolder){
                            ((StaffViewHolder)baseLVHolder).init(getItem(position));
                        }
                    }
                    break;
            }
        return convertView;
    }

    IAddressClick iAddressClick;

    public void setList(List<Staff> list) {
        this.lists = list;
        notifyDataSetChanged();
    }

    public interface IAddressClick{
        void onSearchClick();

        void onCustomerAddressClick();

        void onAddStaffClick();

        void onCompanydepartmentClick();

        void onStaffClick(Staff staff);
    }

    class SearchViewHolder extends BaseLVHolder{

        EditText et_search;
        public SearchViewHolder(View converView) {
            super(converView);
            et_search = (EditText) converView.findViewById(R.id.et_search);
            et_search.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null != iAddressClick){
                        iAddressClick.onSearchClick();
                    }
                }
            });
        }
    }
    class MenuViewHolder extends BaseLVHolder{
        LinearLayout ll_customer_address,ll_add_staff,ll_company_department;
        public MenuViewHolder(View converView) {
            super(converView);
            converView.findViewById(R.id.ll_customer_address).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null != iAddressClick){
                        iAddressClick.onCustomerAddressClick();
                    }
                }
            });
            converView.findViewById(R.id.ll_add_staff).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null != iAddressClick){
                        iAddressClick.onAddStaffClick();
                    }
                }
            });
            converView.findViewById(R.id.ll_company_department).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null != iAddressClick){
                        iAddressClick.onCompanydepartmentClick();
                    }
                }
            });

        }
    }
    class StaffViewHolder extends BaseLVHolder{

        ImageView iv_icon;
        TextView tv_name_job;

        public StaffViewHolder(View converView) {
            super(converView);
            iv_icon = (ImageView) converView.findViewById(R.id.iv_icon);
            tv_name_job = (TextView) converView.findViewById(R.id.tv_name_job);
        }
        public void init(final Staff staff){
            tv_name_job.setText(staff.getName()+"\t|\t"+staff.getJob());
            converView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   if(null != iAddressClick){
                       iAddressClick.onStaffClick(staff);
                   }
                }
            });
        }
    }
}
